const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');

Page({
  data: {
    // sales: [], //明细列表
    page: 1,
    limit: 20,
    total_page: 1,
  },

  onLoad: function(options) {
    this.setData({
			id: options.id != undefined ? options.id:'',
			type: options.type != undefined ? options.type : '',
			id_type: options.id_type != undefined ? options.id_type : '',
      time: options.time,
      start_time: options.start_time,
      end_time: options.end_time
    })
  },

  onShow: function() {
    this.refreshData();
  },

  //刷新数据
  refreshData: function() {
    var that = this;
    this.setData({
      page: 1,
      total_page: 1
    })
    setTimeout(function() {
      that.getList();
    }, 300)
  },

  // 收入明细
  getList: function() {
    var that = this;
    var params = new Object()
    params.page = that.data.page;
    params.limit = that.data.limit;
    params.time = that.data.time;
		if (that.data.end_time) {
			params.start_time = that.data.start_time;
			params.end_time = that.data.end_time;
    }
		if (that.data.id && that.data.id_type) {
			params.id = that.data.id;
			params.id_type = that.data.id_type;
    }

		params.type = that.data.type;
		
    api.POST({
			url: app.globalData.base_url + 'shop/refundDetail',
      params: params,
      success: function(res) {
        util.hiddenLoading();
        wx.stopPullDownRefresh();
        if (res.data.code) {
          util.showError(res.data.message);
          return false;
        }
        var paging = res.data.data.paging;
        that.setData({
          page: paging.page,
          total_page: paging.total_page
        })
        var currentSaleDetail = res.data.data.list;
        if (that.data.page == 1) {
          var saleDetailLists = currentSaleDetail;
        } else {
          var saleDetailLists = that.data.sales.concat(currentSaleDetail);
        }
        that.setData({
          sales: saleDetailLists,
        })
      },
      fail: function(res) {
        util.hiddenLoading();
        wx.stopPullDownRefresh();
      }
    })
  },

  //下拉刷新
  onPullDownRefresh: function() {
    wx.showLoading({
      title: '加载中',
    })
    this.refreshData();
  },

  //加载更多
  onReachBottom: function() {
    var that = this;
    if (this.data.page < this.data.total_page) {
      this.setData({
        page: ++this.data.page
      })
      setTimeout(function() {
        that.getList();
      }, 300)
    }
  }

})